﻿
using GoodDotNet.WebAdmin.Controllers;
using GoodDotNet.WebAdmin.HttpContextUser;
using Microsoft.Extensions.Options;
using GoodDotNet.Data;
using GoodDotNet.Core.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using GoodDotNet.Service;
using GoodDotNet.Core.Data;

namespace GoodDotNet.Webadmin.Controllers
{
    [ApiController]
    [Route("api/admin-module")]
    public class AdminModuleController : BaseController
    {
        public AdminModuleController(IUser user, IHttpContextAccessor accessor, IOptions<List<ConnectionString>> conns, ILogger<BaseController> logger, IOptions<Appsettings> appsettings) : base(user, accessor, conns, logger, appsettings)
        {
        }

        [HttpPost]
        [Route("Add")]
        [Authorize]
        public Result Add([FromBody] ParInsertAdminModuleModel pars)
        {

            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminModuleService service = new AdminModuleService(uniOfWork);
                service.Add(pars,_user.Id);
            }
            return Sucess("sucess");


        }



        [HttpPost]
        [Route("Update")]
        [Authorize]
        public Result Update([FromBody] ParUpdateAdminModuleModel pars)
        {

            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminModuleService service = new AdminModuleService(uniOfWork);
                service.Update(pars,_user.Id);
            }
            return Sucess("sucess");


        }

        [HttpPost]
        [Route("GetOne")]
        [Authorize]
        public Result GetOne([FromBody] ParGetOneAdminModuleModel pars)
        {

            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminModuleService service = new AdminModuleService(uniOfWork);
                var obj = service.GetOne(pars);
                return Sucess(obj);
            }


        }

        [HttpPost]
        [Route("GetList")]
        [Authorize]
        public Result GetList([FromBody] ParGetListAdminModuleModel pars)
        {
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminModuleService service = new AdminModuleService(uniOfWork);
                var obj = service.GetList(pars);
                return Sucess(obj);
            }
        }

        [HttpPost]
        [Route("List")]
        [Authorize]
        public Result List()
        {
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                var list = uniOfWork.AdminModuleRepository.List();
                return Sucess(list);
            }
        }

        [HttpPost]
        [Route("Delete")]
        [Authorize]
        public Result Delete([FromBody] ParDeleteAdminModuleModel pars)
        {

            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminModuleService service = new AdminModuleService(uniOfWork);
                service.Delete(pars,_user.Id);
            }
            return Sucess("sucess");


        }
    }
}
